package com.wuyan.locationdog.view

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.TextView
import com.amap.api.maps.model.LatLng
import com.arcsoft.commonbase.constant.Constants
import com.arcsoft.commonbase.ext.customShowLong
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.util.maputil.MapUtil

import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.Direction
import razerdp.util.animation.TranslationConfig


class MapPopup(context: Context?) : BasePopupWindow(context) {



    init {
        contentView = createPopupById(R.layout.pop_map_third_nav)
        popupGravity = Gravity.BOTTOM

    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        if (!MapUtil.isInstalled(Constants.packageNames[0])) {
            findViewById<TextView>(R.id.btn_tencent).setText("腾讯地图 (未安装)")
        }
        if (!MapUtil.isInstalled(Constants.packageNames[1])) {
            findViewById<TextView>(R.id.btn_gaode).setText("高德地图 (未安装)")
        }
        if (!MapUtil.isInstalled(Constants.packageNames[2])) {
            findViewById<TextView>(R.id.btn_baidu).setText("百度地图 (未安装)")
        }
    }

    override fun onCreateShowAnimation(): Animation? {
        return AnimationHelper.asAnimation()
            .withTranslation(
                TranslationConfig()
                    .from(Direction.BOTTOM)
            )
            .toShow()
    }


    fun mapClick(latitude: Double, longitude: Double) {
        findViewById<TextView>(R.id.btnCancel).setOnClickListener { dismiss() }
        findViewById<TextView>(R.id.btn_tencent).setOnClickListener {
            if (MapUtil.isInstalled(Constants.packageNames[0])) {
                MapUtil.openTencent(context, latitude, longitude, "")

            } else {
                customShowLong("未安装" + (it as TextView).text.toString())
            }
            dismiss()
        }
        findViewById<TextView>(R.id.btn_gaode).setOnClickListener {
            if (MapUtil.isInstalled(Constants.packageNames[1])) {
                MapUtil.openGaoDeNavi(context, latitude.toString(), longitude.toString())
            } else {
                customShowLong("未安装" + (it as TextView).text.toString())
            }
            dismiss()
        }
        findViewById<TextView>(R.id.btn_baidu).setOnClickListener {
            if (MapUtil.isInstalled(Constants.packageNames[2])) {
                val gcJ2BD = MapUtil.GCJ2BD(LatLng(latitude, longitude))
                MapUtil.openBaiduNavi(context, gcJ2BD.latitude.toString(), gcJ2BD.longitude.toString())
            } else {
                customShowLong("未安装" + (it as TextView).text.toString())
            }
            dismiss()
        }
    }
}
